#include <stdio.h>
#include "resources.h"
#include "map.h"
#include "bailout.h"

static BITMAP* safe_load_bitmap(const char* use, const char* filename)
{
	BITMAP* bmp = load_bitmap(filename, NULL);
	if (bmp == NULL) {
		fprintf(stderr, "ERROR: %s Bitmap (%s) didn't load!", use, filename);
	}
	return bmp;
}

static void safe_destroy_bitmap(BITMAP *bmp)
{
	if (bmp != NULL) {
		destroy_bitmap(bmp);
	}
}

static FONT* safe_load_font(const char* use, const char* filename)
{
	FONT* font = load_font(filename, NULL, NULL);
	if (font == NULL) {
		fprintf(stderr, "ERROR: %s Font (%s) didn't load!", use, filename);
	}
	return font;
}

static void safe_destroy_font(FONT* font)
{
	if (font != NULL) {
		destroy_font(font);
	}
}

/**
 * Precondition: Allegro is initialized
 * Please note! some resources like the weapon sheets are loaded
 * on-demand. They must not be accessed directly but with a
 * resources_get_* ()-function that will load the resource if necessary.
 */
void resources_load(void)
{
	memset (&bmp, 0, sizeof(bmp));
	bmp.menu = safe_load_bitmap("Menu Background", "gfx/mainmenu.tga");
	bmp.cursor = safe_load_bitmap ("Cursor", "gfx/cursor.tga");
	bmp.winset = safe_load_bitmap ("GUI graphics", "gfx/gui.tga");
	for (int i = 0; i < TILESET_COUNT; i++) {
		char tname [100]; sprintf (tname, "Tileset %d", i);
		char fname [100]; sprintf (fname, "gfx/tiles%d.tga", i);
		bmp.tileset[i] = safe_load_bitmap(tname, fname);
	}
	bmp.objects = safe_load_bitmap("Objects", "gfx/objects.tga");
	bmp.icons = safe_load_bitmap("Icons", "gfx/items.tga");
	bmp.player = safe_load_bitmap("Player", "gfx/p1_sprites.tga");
	bmp.strtemp = create_bitmap (WIDTH, 20); // Temporary buffer for text (dmg etc.)
	fnt.sans = safe_load_font("Sans", "sans.tga");
	fnt.dmg_font = safe_load_font("Damage", "sans_damage.tga"); // does not work for now
	
	make_trans_font(fnt.sans);
	make_trans_font(fnt.dmg_font);
}

/**
 * Vorbedingung: resources_load wurde erfolgreich ausgeführt
 */
void resources_unload(void)
{
	safe_destroy_bitmap(bmp.menu);
	safe_destroy_bitmap(bmp.cursor);
	safe_destroy_bitmap(bmp.winset);
	safe_destroy_bitmap(bmp.tileset[0]);
	safe_destroy_bitmap(bmp.tileset[1]);
	safe_destroy_bitmap(bmp.tileset[2]);
	safe_destroy_bitmap(bmp.tileset[3]);
	safe_destroy_bitmap(bmp.objects);
	safe_destroy_bitmap(bmp.icons);
	for (int i = 1; i < WEAPONS_RANGE_END; i++) {
		if (bmp.weapons[i] != NULL)
			safe_destroy_bitmap(bmp.weapons[i]);
	}
	safe_destroy_bitmap(bmp.player);
	safe_destroy_bitmap(bmp.strtemp);
	safe_destroy_font(fnt.sans);
	safe_destroy_font(fnt.dmg_font);
	memset (&bmp, 0, sizeof(bmp));
	memset (&fnt, 0, sizeof(fnt));
}

BITMAP* resources_get_weapon (int index)
{
	if ((index < 0) || (index >= WEAPONS_RANGE_END)) 
		bailout ("sds", "Failed to load weapon bitmap for weapon index ", index, "!");
	
	if (bmp.weapons[index] == NULL) {
		char wepfile [256];
		sprintf (wepfile, "gfx/weapons/wep%d.tga", index);
		// bmp.weapons[index] = safe_load_bitmap ("Weapon", wepfile);
		// FIXME: we only got 1 usable weapon sheet so far
		bmp.weapons[index] = safe_load_bitmap ("Weapon", "gfx/weapons/wep38.tga");
	}
	return bmp.weapons[index];
}
